package com.ywjh.inter.servlet.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.PatientSportRecordEntity;
import com.ywjh.inter.entity.SportPlanEntity;
import com.ywjh.inter.entity.SportPlanItemEntity;
import com.ywjh.inter.entity.SportPlanItemTemplateEntity;
import com.ywjh.inter.entity.SportPlanTemplateEntity;
import com.ywjh.inter.service.SportService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Sport extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4118656227928156428L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "add":// 添加计划
			int patient_id = data.getInt("patient_id");
			int doc_id = data.getInt("doc_id");
			String start_time = data.getString("start_time");
			String tips = data.getString("tips");
			String way_array = data.getString("way_array");
			String per_array = data.getString("per_array");
			String time_array = data.getString("time_array");
			String times_array=data.getString("times_array");
			SportPlanEntity sportPlanEntity = new SportPlanEntity();
			sportPlanEntity.setAdd_doc_id(doc_id);
			sportPlanEntity.setStart_time(StrUtil.getTimestamp(start_time));
			sportPlanEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			sportPlanEntity.setPatient_id(patient_id);
			sportPlanEntity.setStatus(1);
			sportPlanEntity.setDiet_tips(tips);
			if (way_array == null || per_array == null || time_array == null||times_array==null) {
				r = 0;
				e = "数据错误";
			} else {
				String[] way = way_array.split(",");
				String[] per = per_array.split(",");
				String[] time = time_array.split(",");
				String[] times=times_array.split(",");
				List<SportPlanItemEntity> list = new LinkedList<>();
				for (int i = 0; i < way.length; i++) {
					SportPlanItemEntity sportPlanItemEntity = new SportPlanItemEntity();
					sportPlanItemEntity.setPer(per[i]);
					sportPlanItemEntity.setTime(Integer.parseInt(time[i]));
					sportPlanItemEntity.setTimes(Integer.parseInt(times[i]));
					sportPlanItemEntity.setWay(way[i]);
					list.add(sportPlanItemEntity);
				}
				int id = SportService.getInstance().addSportPlan(sportPlanEntity, list);
				if (id > 0) {
					r = 1;
				} else {
					r = 0;
					e = "数据库错误";
				}
			}
			break;
		case "update":// 添加计划
			int id=data.getInt("id");
			patient_id = data.getInt("patient_id");
			doc_id = data.getInt("doc_id");
			start_time = data.getString("start_time");
			tips = data.getString("tips");
			way_array = data.getString("way_array");
			per_array = data.getString("per_array");
			time_array = data.getString("time_array");
			times_array=data.getString("times_array");
			sportPlanEntity = new SportPlanEntity();
			sportPlanEntity.setId(id);
			sportPlanEntity.setAdd_doc_id(doc_id);
			sportPlanEntity.setStart_time(StrUtil.getTimestamp(start_time));
			sportPlanEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			sportPlanEntity.setPatient_id(patient_id);
			sportPlanEntity.setStatus(1);
			sportPlanEntity.setDiet_tips(tips);
			if (way_array == null || per_array == null || time_array == null||times_array==null) {
				r = 0;
				e = "数据错误";
			} else {
				String[] way = way_array.split(",");
				String[] per = per_array.split(",");
				String[] time = time_array.split(",");
				String[] times=times_array.split(",");
				List<SportPlanItemEntity> list = new LinkedList<>();
				for (int i = 0; i < way.length; i++) {
					SportPlanItemEntity sportPlanItemEntity = new SportPlanItemEntity();
					sportPlanItemEntity.setPer(per[i]);
					sportPlanItemEntity.setTime(Integer.parseInt(time[i]));
					sportPlanItemEntity.setTimes(Integer.parseInt(times[i]));
					sportPlanItemEntity.setWay(way[i]);
					list.add(sportPlanItemEntity);
				}
				boolean b = SportService.getInstance().updateSportPlan(sportPlanEntity, list);
				if (b) {
					r = 1;
				} else {
					r = 0;
					e = "数据库错误";
				}
			}
			break;
		case "add_template":// 添加模版
			doc_id = data.getInt("doc_id");
			tips = data.getString("tips");
			String name=data.getString("name");
			SportPlanTemplateEntity sportPlanTemplateEntity = new SportPlanTemplateEntity();
			sportPlanTemplateEntity.setName(name);
			sportPlanTemplateEntity.setAdd_doc_id(doc_id);
			sportPlanTemplateEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			sportPlanTemplateEntity.setStatus(1);
			sportPlanTemplateEntity.setDiet_tips(tips);
			way_array = data.getString("way_array");
			per_array = data.getString("per_array");
			time_array = data.getString("time_array");
			times_array=data.getString("times_array");
			if (way_array == null || per_array == null || time_array == null||times_array==null) {
				r = 0;
				e = "数据错误";
			} else {
				String[] way = way_array.split(",");
				String[] per = per_array.split(",");
				String[] time = time_array.split(",");
				String[] times=times_array.split(",");
				List<SportPlanItemTemplateEntity> list = new LinkedList<>();
				for (int i = 0; i < way.length; i++) {
					SportPlanItemTemplateEntity sportPlanItemEntity = new SportPlanItemTemplateEntity();
					sportPlanItemEntity.setPer(per[i]);
					sportPlanItemEntity.setTime(Integer.parseInt(time[i]));
					sportPlanItemEntity.setTimes(Integer.parseInt(times[i]));
					sportPlanItemEntity.setWay(way[i]);
					list.add(sportPlanItemEntity);
				}
				boolean b = SportService.getInstance().addSportPlanTemplate(sportPlanTemplateEntity, list);
				if (b) {
					r = 1;
				} else {
					r = 0;
					e = "数据库错误";
				}
			}
			break;
		case "update_template":// 添加模版
			id=data.getInt("id");
			doc_id = data.getInt("doc_id");
			tips = data.getString("tips");
			name=data.getString("name");
			sportPlanTemplateEntity = new SportPlanTemplateEntity();
			sportPlanTemplateEntity.setId(id);
			sportPlanTemplateEntity.setName(name);
			sportPlanTemplateEntity.setAdd_doc_id(doc_id);
			sportPlanTemplateEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			sportPlanTemplateEntity.setStatus(1);
			sportPlanTemplateEntity.setDiet_tips(tips);
			way_array = data.getString("way_array");
			per_array = data.getString("per_array");
			time_array = data.getString("time_array");
			times_array=data.getString("times_array");
			if (way_array == null || per_array == null || time_array == null||times_array==null) {
				r = 0;
				e = "数据错误";
			} else {
				String[] way = way_array.split(",");
				String[] per = per_array.split(",");
				String[] time = time_array.split(",");
				String[] times=times_array.split(",");
				List<SportPlanItemTemplateEntity> list = new LinkedList<>();
				for (int i = 0; i < way.length; i++) {
					SportPlanItemTemplateEntity sportPlanItemEntity = new SportPlanItemTemplateEntity();
					sportPlanItemEntity.setPer(per[i]);
					sportPlanItemEntity.setTime(Integer.parseInt(time[i]));
					sportPlanItemEntity.setTimes(Integer.parseInt(times[i]));
					sportPlanItemEntity.setWay(way[i]);
					list.add(sportPlanItemEntity);
				}
				boolean b = SportService.getInstance().updateSportPlanTemplate(sportPlanTemplateEntity, list);
				if (b) {
					r = 1;
				} else {
					r = 0;
					e = "数据库错误";
				}
			}
			break;
		case "list_template":// 查询所有模版
			doc_id = data.getInt("doc_id");
			JSONArray array = new JSONArray();
			List<SportPlanTemplateEntity> tempList = SportService.getInstance().listTemplateByDocId(doc_id);
			for (SportPlanTemplateEntity tempEntity : tempList) {
				JSONObject tempjson = tempEntity.toJson();
				List<SportPlanItemTemplateEntity> tempItemList = SportService.getInstance().listItemTemplateByPlanId(tempEntity.getId());
				JSONArray itemArray=new JSONArray();
				for (SportPlanItemTemplateEntity tempItemEntity : tempItemList) {
					itemArray.add(tempItemEntity.toJson());
				}
				tempjson.put("item_array", itemArray);
				array.add(tempjson);
			}
			r = 1;
			json.put("d", array);
			break;
		case "list_sport_plan_by_patient_id":// 查看患者饮食计划
			patient_id = data.getInt("patient_id");
			doc_id = data.getInt("doc_id");
			array = new JSONArray();
			List<SportPlanEntity> planList = SportService.getInstance().listByPatientId(patient_id, doc_id);
			for (SportPlanEntity entity : planList) {
				JSONObject tempjson = entity.toJson();
				List<SportPlanItemEntity> itemList = SportService.getInstance().listItemByPlanId(entity.getId());
				JSONArray itemArray=new JSONArray();
				for (SportPlanItemEntity itemEntity : itemList) {
					itemArray.add(itemEntity.toJson());
				}
				tempjson.put("item_array", itemArray);
				array.add(tempjson);
			}
			r = 1;
			json.put("d", array);
			break;
		case "list_sport_patient":// 查看患者执行
			int sport_plan_id = data.getInt("sport_plan_id");
			array = new JSONArray();
			List<PatientSportRecordEntity> sportPatientList = SportService.getInstance().listSportPatientByPlanId(sport_plan_id);
			for (PatientSportRecordEntity sportRecordEntity : sportPatientList) {
				JSONObject dpjson = sportRecordEntity.toJson();
				array.add(dpjson);
			}
			r = 1;
			json.put("d", array);
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();

	}
}
